
var data = [];

$(function() {	
	Tip.emp('借款人');
});

var BorrowSun = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"添加": function() {				
				if (!confirm("确认是否添加？")) {
					return false;
				}				
	
				var form = $("#form_add")[0];
	
				var body = { };				
				body.emp = form.emp.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				
				body.payment = form.payment.value;
				
				var payment_x = {};
				if (body.payment == '2') {
					payment_x.bank = form.x_bank.value;
					payment_x.name = form.x_name.value;
					payment_x.code = form.x_code.value;
					payment_x.content = form.x_content.value;
				}
				
				body.payment_x = $.toJSON(payment_x);
				
				body.type = form.type.value;
				body.begin = form.date1.value;
				body.end = form.date2.value;
				body.day = form.day.value;
									
				$(this).dialog("close");
	
				$.post('finance.php?do=BorrowSun.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	dialog_batch: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {	
			"关闭": function() { 
				$(this).dialog("close");
			},			
			
			"提交": function() {				
				if (!confirm("确认是否提交？")) {
					return false;
				}				
	
				var form = $("#form_add")[0];
	
				var body = { };
				
				body.type = form.type.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.begin = form.date1.value;
				body.end = form.date2.value;
				// body.day = form.day.value;
				
				// body.item = data;
				body.item = $.toJSON(data);
									
				$(this).dialog("close");
	
				$.post('finance.php?do=BorrowSun.batchPost', body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_add(url, title, xy);
	},
	
	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=BorrowSun.edit&id=' + id;
		var c2 = 'finance.php?do=BorrowSun.view&id=' + id;
		var c3 = 'finance.php?do=BorrowSun.copy&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'BorrowSun.dialog_edit_', '600x380'];	
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '600x380'];
		
		if (status == '1') {
			var d1 = 'finance.php?do=BorrowSun.verify&id=' + id;
			var b1 = ['审核借款', 'url', 'BorrowSun.dialog_verify', '650x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}
		
		if (status == '2') {
			var s = c1 + ", " + c2;

			return this.popup_(e, [a1, a2], s);
		}

		var s = c1 + ", " + c2;

		return this.popup_(e, [a1, a2], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c1 = 'finance.php?do=BorrowSun.upload&id=' + id;
		var a1 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c2 = 'finance.php?do=BorrowSun.view&print=print&id=' + id;
		var a2 = ['打印信息', 'url', 'Common.dialog_view_', '742x508'];
		
		var c3 = 'finance.php?do=BorrowSun.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'BorrowSun.dialog_copy_', '700x450'];
		
		var ss = 'finance.php?do=BorrowSun.ss&id=' + id;
		var bb = ['修改状态', 'url', 'BorrowSun.dialog_ss', '400x200'];
		
		s += ", " + c1 + ", " + c2 + ", " + c3 + ", " + ss;
		
		a.push(a1);
		a.push(a2);
		a.push(a3);
		a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=BorrowSun.audit&item=145', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'finance.php?do=BorrowSun.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},
	
	dialog_verify: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=BorrowSun.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意借款？")) {
							return false;
						}				
						
						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 9;
						
						$(this).dialog("close");
						
						var url = 'finance.php?do=BorrowSun.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意借款？")) {
							return false;
						}				

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 2;
						
						$(this).dialog("close");

						var url = 'finance.php?do=BorrowSun.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.id = form.id.value;
				body.emp = form.emp.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				
				body.payment = form.payment.value;
				
				var payment_x = {};
				if (body.payment == '2') {
					payment_x.bank = form.x_bank.value;
					payment_x.name = form.x_name.value;
					payment_x.code = form.x_code.value;
					payment_x.content = form.x_content.value;
				}
				
				body.payment_x = $.toJSON(payment_x);
				
				body.type = form.type.value;
				body.begin = form.date1.value;
				body.end = form.date2.value;
				body.day = form.day.value;
										
				$(this).dialog("close");
	
				var url = 'finance.php?do=BorrowSun.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {			
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}
	
				var form = $("#form_edit")[0];
				
				var body = { };
				body.emp = form.emp.value;
				
				body.name = form.name.value;
				body.content = form.content.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				
				body.payment = form.payment.value;
				
				var payment_x = {};
				if (body.payment == '2') {
					payment_x.bank = form.x_bank.value;
					payment_x.name = form.x_name.value;
					payment_x.code = form.x_code.value;
					payment_x.content = form.x_content.value;
				}
				
				body.payment_x = $.toJSON(payment_x);
				
				body.type = form.type.value;
				body.begin = form.date1.value;
				body.end = form.date2.value;
				body.day = form.day.value;
										
				$(this).dialog("close");
	
				var url = 'finance.php?do=BorrowSun.addPost';
	
				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});
	
		dialog_edit(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=BorrowSun.delete' + p2;
		var c2 = 'finance.php?do=BorrowSun.txt' + p2;
		var c3 = 'finance.php?do=BorrowSun.update' + p2;
		var c4 = 'finance.php?do=BorrowSun.batch_copy' + p2;
		
		var c9 = 'finance.php?do=BorrowSun.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];
		var a2 = ['转帐信息', 'url', 'Common.dialog_txt', '650x400'];
		
		var a3 = ['编辑信息', 'url', 'BorrowSun.dialog_update', '750x450'];
		var a4 = ['复制信息', 'url', 'BorrowSun.dialog_batch_copy', '750x450'];
		
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c4 + ", " + c9;

		return popup(e, [a1, a2, a3, a4, a9], s);
	},
	
	dialog_update: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=BorrowSun.audit&item=152', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"提交": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}
						
						// var cc = Produce._cc();

						var form = $("#form_edit")[0];
						
						var body = {};
						
						body.type = form.type.value;
						
						body.name = form.name.value;
						body.content = form.content.value;
						
						body.begin = form.date1.value;
						body.end = form.date2.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
						
						var url = 'finance.php?do=BorrowSun.updatePost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	},
	
	dialog_batch_copy: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('finance.php?do=BorrowSun.audit&item=152', function(json) {
			if (json.status == 1) {			
				var c = new Array();
				$("input[name^='c_']").each(function() {
					if ($(this).attr("checked")) {
						var s = encodeURIComponent(this.name) + "=" + encodeURIComponent($(this).val());
						c.push(s);
					}
				});
				
				var body = c.join("&");
				
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},					
					
					"复制": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						
						body.type = form.type.value;
						
						body.name = form.name.value;
						body.content = form.content.value;
						
						body.begin = form.date1.value;
						body.end = form.date2.value;
						
						// body.item = data;
						body.item = $.toJSON(data);
						
						var url = 'finance.php?do=BorrowSun.batchPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				var view = '#dialog_edit';
				$.get(url, body, function(data) {
					$(view).html(data);
				});

				var regex = /^(\d+)(x)(\d+)$/;
				if (regex.test(xy)) {
					var cc = xy.split("x");
					var width = parseInt(cc[0]);
					var height = parseInt(cc[1]);
					$(view).dialog('option', 'height', height);
				} else {
					var width = parseInt(xy);
				}

				$(view).dialog('option', 'title', title);
				$(view).dialog('option', 'width', width);
				$(view).dialog('open');
			} else {
				_error();
			}
		});	
	}
};
